package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.CartInfoService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CartController {
    @Autowired
    private CartInfoService cartService;
    @Operation(summary = "往购物车加商品")
    @GetMapping("addToCart/{skuId}/{num}")
    public AjaxResult addtocart(  @PathVariable("skuId") Long skuId,
                                  @PathVariable("num") Integer num){
        cartService.addtocart(skuId,num);
        return AjaxResult.success();
    }

    @Operation(summary = "显示商品")
    @GetMapping("cartList")
    public AjaxResult cartlist(){
        List<CartInfo> list= cartService.cartlist();
        return AjaxResult.success(list);
    }

    @Operation(summary = "更新选中状态")
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public AjaxResult checkcart(@PathVariable("skuId") Long skuId,
                                @PathVariable("isChecked") Integer isChecked){
        cartService.checkcart(skuId,isChecked);
        return AjaxResult.success();
    }

    @Operation(summary = "选中所有商品的状态")
    @GetMapping("allCheckCart/{isChecked}")
    public AjaxResult allcheckcart(@PathVariable("isChecked") Integer isChecked){
        cartService.callcheckcart(isChecked);
        return AjaxResult.success();
    }
    @Operation(summary = "删除某个商品")
    @DeleteMapping("deleteCart/{skuId}")
    public AjaxResult deletecart(@PathVariable("skuId") Long skuId){
        cartService.deletecart(skuId);
        return AjaxResult.success();
    }

    @Operation(summary = "清空购物车")
    @GetMapping("clearCart")
    public AjaxResult clearcart(){
        cartService.clearcart();
        return AjaxResult.success();
    }

    @Operation(summary = "查询购物车中选中的商品")
    @InnerAuth
    @GetMapping("/getCartCheckedList")
    public R<List<CartInfo>> getcartcheckedlist(){
        return R.ok(cartService.getcartcheckedlist());
    }

    @Operation(summary="更新用户购物车列表价格")
    @InnerAuth
    @GetMapping("/updateCartPrice")
    public R<Boolean> updateCartPrice(){
        return R.ok(cartService.updateCartPrice());
    }

    @Operation(summary="删除用户购物车列表中选中商品列表")
    @InnerAuth
    @GetMapping("/deleteCartCheckedList")
    public R<Boolean> deleteCartCheckedList(){
        return R.ok(cartService.deleteCartCheckedList());
    }
}
